var apkKist = []
// #ifdef APP-PLUS

plus.android.importClass('java.util.ArrayList');
plus.android.importClass('android.content.pm.PackageInfo');
plus.android.importClass('android.content.pm.PackageManager');
var ApplicationInfo = plus.android.importClass('android.content.pm.ApplicationInfo');
var MainActivity = plus.android.runtimeMainActivity();
var PackageManager = MainActivity.getPackageManager();

plus.android.importClass('android.graphics.drawable.BitmapDrawable');
var BitmapFactory = plus.android.importClass("android.graphics.BitmapFactory");
var Base64 = plus.android.importClass("android.util.Base64");
var Bitmap = plus.android.importClass('android.graphics.Bitmap');
var ByteArrayOutputStream = plus.android.importClass("java.io.ByteArrayOutputStream");

// #endif

async function getAppList() {

	apkKist = []
	// #ifdef APP-PLUS
	var pinfo = plus.android.invoke(PackageManager, 'getInstalledPackages', 0)

	if (pinfo != null) {
		for (var i = 0; i < pinfo.size(); i++) {
			//PackageInfo{4b45699f9d  com.tencent.mobileqq}      
			var pkginfo = pinfo.get(i);
			var issysapk = ((pkginfo.plusGetAttribute("applicationInfo").plusGetAttribute("flags") &
				ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false
			if (issysapk == false) {
				const apkinfo = {
					name: pkginfo.plusGetAttribute("applicationInfo").loadLabel(PackageManager)
						.toString(),
					packageName: pkginfo.plusGetAttribute("packageName"),
					// versionName: pkginfo.plusGetAttribute("versionName"),  
					// versionCode: pkginfo.plusGetAttribute("versionCode"),  
					appIco: pkginfo.plusGetAttribute("applicationInfo").loadIcon(PackageManager)
				}

				var bimp = apkinfo.appIco.getBitmap();
				try {
					var baos = new ByteArrayOutputStream();
					bimp.compress(Bitmap.CompressFormat.PNG, 100, baos);
					baos.flush();
					baos.close();
					var bitmapBytes = baos.toByteArray();
					var result = "data:image/png;base64," + Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
					apkinfo.img = result;
				} catch (e) {

				}
				apkKist.push(apkinfo);
			};
		};
	};
	// #endif

	return apkKist
}

function getCachedAppList() {
	return apkKist
}

function uninstallApp(packageName) {
	var Intent = plus.android.importClass("android.content.Intent");
	var Uri = plus.android.importClass('android.net.Uri');
	var main = plus.android.runtimeMainActivity();
	var _intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package", packageName, null))

	main.startActivity(_intent);
}


function autoUpdateAppList() {
	var main = plus.android.runtimeMainActivity(); //获取activity  
	var context = plus.android.importClass('android.content.Context'); //上下文  
	var receiver = plus.android.implements('android.content.BroadcastReceiver', {
		onReceive: () => {
			console.log("App list change")
			getAppList()
		}
	});
	var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //引入过滤器  
	var Intent = plus.android.importClass('android.content.Intent');
	var filter = new IntentFilter();

	filter.addAction("android.intent.action.PACKAGE_ADDED"); //监听应用安装
	filter.addAction("android.intent.action.PACKAGE_REMOVED"); //监听应用安装
	main.registerReceiver(receiver, filter); //注册监听  
	console.log("注册了应用列表监听器")

}

function launchApp(packageName) {
	plus.runtime.launchApplication({ pname:packageName  })
}

export {
	getCachedAppList,
	uninstallApp,
	autoUpdateAppList,
	getAppList,
	launchApp
}
